using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VVPBOM
    {
        public DataRow Convert2VPBOM(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryVPBOM(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["VPBMANDANT"] = " ".PadRight(2 ); 
         dwReturn["VPBPART"] = " ".PadRight(20 ); 
         dwReturn["VPBTAX"] = " ".PadRight(3 ); 
         dwReturn["VPBPOS"] = " ".PadRight(4 ); 
         dwReturn["VPBWAKZ"] = " ".PadRight(1 ); 
         dwReturn["VPBKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["VPBWERK"] = " ".PadRight(3 ); 
         dwReturn["VPBABT"] = " ".PadRight(3 ); 
         dwReturn["VPBART"] = " ".PadRight(20 ); 
         dwReturn["VPBVAR"] = " ".PadRight(3 ); 
         dwReturn["VPBFARBE"] = " ".PadRight(10 ); 
         dwReturn["VPBDESS"] = " ".PadRight(10 ); 
         dwReturn["VPBCOLORIT"] = " ".PadRight(10 ); 
         dwReturn["VPBBESCHICH"] = " ".PadRight(10 ); 
         dwReturn["VPBZUSTAND"] = " ".PadRight(4 ); 
         dwReturn["VPBGROE"] = " ".PadRight(7 ); 
         dwReturn["VPBAUFM"] = " ".PadRight(2 ); 
         dwReturn["VPBVERP"] = " ".PadRight(4 ); 
         dwReturn["VPBANT"] = " ".PadRight(0 ); 
         dwReturn["VPBQTY"] = " ".PadRight(0 ); 
         dwReturn["VPBGQTY"] = " ".PadRight(0 ); 
         dwReturn["VPBFILLER"] = " ".PadRight(40 ); 
         dwReturn["VPBAENZL"] = " ".PadRight(0 ); 
         dwReturn["VPBAENB"] = " ".PadRight(8 ); 
         dwReturn["VPBAEND"] = " ".PadRight(0 ); 
         dwReturn["VPBNEUB"] = " ".PadRight(8 ); 
         dwReturn["VPBNEUD"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapVPBOM(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveVPBOM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryVPBOM(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapVPBOM(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertVPBOM(dwLocal, ITXDBVERSION);
            }
            UpdateVPBOM(dwLocal);
        }
        public void InsertVPBOM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO VPBOM(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryVPBOM(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM VPBOM WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM VPBOM  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "VPBOM";
            Return = Result;
            return Return;
        }
        public void DeleteVPBOM(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM VPBOM   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateVPBOM(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE VPBOM SET "
                    +"VPBMANDANT= :VPBMANDANT,"
                    +"VPBPART= :VPBPART,"
                    +"VPBTAX= :VPBTAX,"
                    +"VPBPOS= :VPBPOS,"
                    +"VPBWAKZ= :VPBWAKZ,"
                    +"VPBKUNDNR= :VPBKUNDNR,"
                    +"VPBWERK= :VPBWERK,"
                    +"VPBABT= :VPBABT,"
                    +"VPBART= :VPBART,"
                    +"VPBVAR= :VPBVAR,"
                    +"VPBFARBE= :VPBFARBE,"
                    +"VPBDESS= :VPBDESS,"
                    +"VPBCOLORIT= :VPBCOLORIT,"
                    +"VPBBESCHICH= :VPBBESCHICH,"
                    +"VPBZUSTAND= :VPBZUSTAND,"
                    +"VPBGROE= :VPBGROE,"
                    +"VPBAUFM= :VPBAUFM,"
                    +"VPBVERP= :VPBVERP,"
                    +"VPBANT= :VPBANT,"
                    +"VPBQTY= :VPBQTY,"
                    +"VPBGQTY= :VPBGQTY,"
                    +"VPBFILLER= :VPBFILLER,"
                    +"VPBAENZL= :VPBAENZL,"
                    +"VPBAENB= :VPBAENB,"
                    +"VPBAEND= :VPBAEND,"
                    +"VPBNEUB= :VPBNEUB,"
                    +"VPBNEUD= :VPBNEUD,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("VPBMANDANT", OracleType.NVarChar).Value = dwData["VPBMANDANT"].ToString(); 
          or1.Parameters.Add("VPBPART", OracleType.NVarChar).Value = dwData["VPBPART"].ToString(); 
          or1.Parameters.Add("VPBTAX", OracleType.NVarChar).Value = dwData["VPBTAX"].ToString(); 
          or1.Parameters.Add("VPBPOS", OracleType.NVarChar).Value = dwData["VPBPOS"].ToString(); 
          or1.Parameters.Add("VPBWAKZ", OracleType.NVarChar).Value = dwData["VPBWAKZ"].ToString(); 
          or1.Parameters.Add("VPBKUNDNR", OracleType.NVarChar).Value = dwData["VPBKUNDNR"].ToString(); 
          or1.Parameters.Add("VPBWERK", OracleType.NVarChar).Value = dwData["VPBWERK"].ToString(); 
          or1.Parameters.Add("VPBABT", OracleType.NVarChar).Value = dwData["VPBABT"].ToString(); 
          or1.Parameters.Add("VPBART", OracleType.NVarChar).Value = dwData["VPBART"].ToString(); 
          or1.Parameters.Add("VPBVAR", OracleType.NVarChar).Value = dwData["VPBVAR"].ToString(); 
          or1.Parameters.Add("VPBFARBE", OracleType.NVarChar).Value = dwData["VPBFARBE"].ToString(); 
          or1.Parameters.Add("VPBDESS", OracleType.NVarChar).Value = dwData["VPBDESS"].ToString(); 
          or1.Parameters.Add("VPBCOLORIT", OracleType.NVarChar).Value = dwData["VPBCOLORIT"].ToString(); 
          or1.Parameters.Add("VPBBESCHICH", OracleType.NVarChar).Value = dwData["VPBBESCHICH"].ToString(); 
          or1.Parameters.Add("VPBZUSTAND", OracleType.NVarChar).Value = dwData["VPBZUSTAND"].ToString(); 
          or1.Parameters.Add("VPBGROE", OracleType.NVarChar).Value = dwData["VPBGROE"].ToString(); 
          or1.Parameters.Add("VPBAUFM", OracleType.NVarChar).Value = dwData["VPBAUFM"].ToString(); 
          or1.Parameters.Add("VPBVERP", OracleType.NVarChar).Value = dwData["VPBVERP"].ToString(); 
          or1.Parameters.Add("VPBANT", OracleType.NVarChar).Value = dwData["VPBANT"].ToString(); 
          or1.Parameters.Add("VPBQTY", OracleType.NVarChar).Value = dwData["VPBQTY"].ToString(); 
          or1.Parameters.Add("VPBGQTY", OracleType.NVarChar).Value = dwData["VPBGQTY"].ToString(); 
          or1.Parameters.Add("VPBFILLER", OracleType.NVarChar).Value = dwData["VPBFILLER"].ToString(); 
          or1.Parameters.Add("VPBAENZL", OracleType.NVarChar).Value = dwData["VPBAENZL"].ToString(); 
          or1.Parameters.Add("VPBAENB", OracleType.NVarChar).Value = dwData["VPBAENB"].ToString(); 
          or1.Parameters.Add("VPBAEND", OracleType.NVarChar).Value = dwData["VPBAEND"].ToString(); 
          or1.Parameters.Add("VPBNEUB", OracleType.NVarChar).Value = dwData["VPBNEUB"].ToString(); 
          or1.Parameters.Add("VPBNEUD", OracleType.NVarChar).Value = dwData["VPBNEUD"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
